package com.qf.java2106.huangshengrui.hotel.dao.impl;

import com.qf.java2106.huangshengrui.hotel.dao.IFoodTypeDao;
import com.qf.java2106.huangshengrui.hotel.pojos.Food;
import com.qf.java2106.huangshengrui.hotel.pojos.FoodType;
import com.qf.java2106.huangshengrui.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author q
 * @version V1.0
 * @date 2021/10/25
 **/
public class FoodTypeDaoImpl implements IFoodTypeDao {

    private QueryRunner queryRunner = null;

    @Override
    public List<FoodType> findAllType() throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT type_id typeId, type_name typeName FROM t_food_type";
        return queryRunner.query(JdbcUtils.getConnection(),sql,new BeanListHandler<>(FoodType.class));
    }

    @Override
    public List<FoodType> findTypeLikeKey(String key) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT type_id typeId, type_name typeName FROM t_food_type WHERE type_name LIKE ?";
        return queryRunner.query(JdbcUtils.getConnection(),sql,new BeanListHandler<>(FoodType.class),key);
    }

    @Override
    public Integer saveType(FoodType foodType) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = " INSERT INTO t_food_type (type_name) VALUE (?)";
        return queryRunner.update(JdbcUtils.getConnection(),sql, foodType.getTypeName());
    }

    @Override
    public FoodType findTypeById(Integer id) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT type_id typeId, type_name typeName FROM t_food_type WHERE type_id = ?";
        return queryRunner.query(JdbcUtils.getConnection(),sql,new BeanHandler<>(FoodType.class),id);
    }

    @Override
    public Integer updateType(FoodType foodType) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "UPDATE t_food_type SET type_id= ?, type_name=? WHERE type_id=?";
        return queryRunner.update(JdbcUtils.getConnection(),sql,foodType.getTypeId(),foodType.getTypeName(),foodType.getTypeId());
    }

    @Override
    public Integer deleteType(Integer id) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "DELETE FROM t_food_type WHERE type_id= ?";
        return queryRunner.update(JdbcUtils.getConnection(),sql,id);
    }

    @Override
    public FoodType findTypeByName(String typeName) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT type_id typeId, type_name typeName FROM t_food_type WHERE type_name = ?";
        return queryRunner.query(JdbcUtils.getConnection(),sql,new BeanHandler<>(FoodType.class),typeName);
    }

    @Override
    public List<Food> checkDeleteType(Integer id) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "SELECT food_id FROM t_food WHERE type_id = ?";
        return queryRunner.query(JdbcUtils.getConnection(),sql,new BeanListHandler<>(Food.class),id);
    }
}
